package com.codeoss.module.purchase.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.purchase.dao.PurchaseRequisitionMapper;
import com.codeoss.module.purchase.model.PurchaseRequisition;
import com.codeoss.module.purchase.model.PurchaseRequisitionLine;
import com.codeoss.module.purchase.service.IPurchaseRequisitionLineService;
import com.codeoss.module.purchase.service.IPurchaseRequisitionService;

@Service
public class PurchaseRequisitionService extends BaseService<PurchaseRequisitionMapper, PurchaseRequisition>
		implements IPurchaseRequisitionService {
	@Autowired
	private IPurchaseRequisitionLineService purchaseRequisitionLineService;
	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<PurchaseRequisitionLine> purchaseRequisitionLineList=purchaseRequisitionLineService.getLineListByPurchaseRequistionId(id);
		if(purchaseRequisitionLineList==null ||purchaseRequisitionLineList.size()==0 ) {
			throw new RuntimeException("没有行明细");
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		PurchaseRequisition purchaseRequisition=getById(id);
		purchaseRequisition.setApproved(true);
		update(purchaseRequisition);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		PurchaseRequisition purchaseRequisition=getById(id);
		purchaseRequisition.setApproved(false);
		update(purchaseRequisition);
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		PurchaseRequisition purchaseRequisition=getById(id);
		purchaseRequisition.setDocStatus("CO");;
		update(purchaseRequisition);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		PurchaseRequisition purchaseRequisition=getById(id);
		purchaseRequisition.setDocStatus(docStatus);;
		update(purchaseRequisition);
	}
}